var categoryMap = {
	1:"创意设计",
	2:"基础部件",
	3:"整机套装",
	4:"应用配套"
};

var academyMap = {
	1:"中国科学院",
	2:"中国工程院",
	3:"国际欧亚科学院中国科学中心",
	4:"发展中国家科学院",
	99:"其它"
};

// 添加奖励
layui.use(['form', 'code', 'laydate','upload','layer','laytpl','element','laypage'], function() {
	var laydate = layui.laydate;
	form = layui.form;
	upload = layui.upload;
	layer = layui.layer;
	laytpl = layui.laytpl;
	element = layui.element;
	laypage = layui.laypage;



	layui.code();


	function lookApplicantInfo(dom){
		var projectId = $(dom.target).attr("data-id");
		var projectList = $(".projectList").data("projectList");
		var applicantBase =  null;
		$(".detailDiv").html(JSON.stringify(projectList));
		for(let i =0;i < projectList.length;i++){
			if(projectId == projectList[i].id){
				applicantBase = projectList[i];
				break;
			}
		}
		// $(".projectId").val(applyProject.id);
		//申请人信息
		$(".name").val(applicantBase.name);
		$(".sex").val(applicantBase.sex == 1?"男":"女");
		$(".photo").prop("src",applicantBase.photo);
		$(".registeredAddress").val(applicantBase.registeredProvince + applicantBase.registeredCity + applicantBase.registeredArea);
		$(".liveAddress").val(applicantBase.liveProvince + applicantBase.liveCity + applicantBase.liveArea + " " + applicantBase.address);
		//项目信息
		$(".realName").val(applicantBase.certificateInfo.realName);
		$(".idcardNum").val(applicantBase.certificateInfo.idcardNum);
		$(".idFrontImage").prop("src",applicantBase.certificateInfo.idFrontImage);
		$(".idBackImage").prop("src",applicantBase.certificateInfo.idBackImage);



		$("#myModal").show();
	}




	function  setSelect(dom,data){
		for(let item in data){
			$(dom).append("<option value='" + item +"'>"+ data[item] +"</option>")
		}
		form.render("select");
	}



// 添加教育经验
	$(".add_btn").on("click", function() {
		$("#myModal").show();
		$("#myModal input, #myModal textarea").val("");

	});


//设置二级分类
	AJAX.query(cgiMain.getDict,
		{type:5},
		function (res) {
			if(res.code=='0000'){
				setSelect($("select[name='secondCategory']"), res.result);
				form.render('select');
				form.on("select(secondCategory)",function (data) {
					$("select[name='thirdCategory']").html("");
					setThirdCategorySelect(data.value);
				});
			}
		});

	function setFourthCategory(thirdCategory) {
		$(".fourthCategoryDiv").hide();
		$(".fourthCategoryItemDiv").html("");
		if(!thirdCategory)return;
		AJAX.query(cgiMain.getDict,
			{type:5,parentId:thirdCategory},
			function (res) {
				if(res.code == "0000"){
					$(".fourthCategoryDiv").show();
					for(let item in res.result){
						$(".fourthCategoryItemDiv").append("<input type='radio' name='fourthCategory'  value='" + item +"' title='"+ res.result[item] +"' lay-filter='fourthCategory'/>")
					}
					form.render();

				}
			});
	}
	function setThirdCategorySelect(secondCategory,defaultChoice){
		AJAX.query(cgiMain.getDict,
			{type:5,parentId:secondCategory},
			function (res) {
				if(res.code=='0000'){
					setSelect($("select[name='thirdCategory']"), res.result);
					form.render('select');
					if(defaultChoice){
						// $(".thirdCategory").val(33);
						form.render('select');
					}

					setFourthCategory($(".thirdCategory").val());
					form.on("select(thirdCategory)",function (data) {
						setFourthCategory($(".thirdCategory").val());
					});

				}
			});
	}

	//初始化列表
	setTimeout(search,500);

	function search(){
		$(".projectList").html("");
		var searchProjectName = $(".searchProjectName").val();
		AJAX.post(cgiMain.applicantAllApplicant,
			{pageNum:1,pageSize:10,searchName:searchProjectName},
			function (res) {

				if(res.code == '0000'){
					$(".projectList").data("projectList",res.result.list);
					var tpl = $("#projectItemTpl").html();
					var resultPage = res.result;
					for(var i = 0; i < resultPage.list.length;i++){
						var item = resultPage.list[i];
						if(item.sex==1){
							item.sexName = "男";
						}else{
							item.sexName = "女";
						}
						item.projectCategoryName = categoryMap[item.projectCategory];
						laytpl(tpl).render(item,function (html) {
							$(".projectList").append(html);

							$(".projectList").on("click",'.lookApplicantInfo',function (elem) {
								lookApplicantInfo(elem);
							});


						});
					}

					//执行一个laypage实例
					laypage.render({
						elem: 'projectPagination'
						,limit: 10
						,limits:[10]
						,theme:'#1E9FFF'
						,layout:['prev', 'page', 'next','limit','count']
						,count: resultPage.totalCount
						,jump: function (obj,first) {
							if(first == true){
								return;
							}
							$(".projectList").html("");
							nextPage(obj.curr,10);

						}
					});
					element.render(".projectList");
				}

			});
	}

	$(".search").click(function () {
		console.log("haha");
		search();
		return false;
	});

	function nextPage(pageNum,pageSize) {
		var searchProjectName = $(".searchProjectName").val();
		AJAX.post(cgiMain.applicantAllApplicant,
			{pageNum:pageNum,pageSize:pageSize,searchName:searchProjectName},
			function (res) {
				if(res.code == '0000'){
					$(".projectList").data("projectList",res.result.list);
					var tpl = $("#projectItemTpl").html();
					var resultPage = res.result;
					for(var i = 0; i < resultPage.list.length;i++){
						var item = resultPage.list[i];
						if(item.sex==1){
							item.sexName = "男";
						}else{
							item.sexName = "女";
						}
						item.projectCategoryName = categoryMap[item.projectCategory];
						laytpl(tpl).render(item,function (html) {
							$(".projectList").append(html);

							$(".projectList").on("click",'.lookApplyInfo',function (elem) {
								lookApplyInfo(elem);
							});


						});
					}

					element.render(".projectList");
				}

			});
	}



});

$(".modal-footer > .layui-btn-primary").on("click",function () {

	closeModal();
});
$(".close").on("click",function () {
	closeModal();
});

function closeModal() {
	$("#myModal").hide();
	$("#myModal input, #myModal textarea").val("");
	$("#myModal img").prop("src","");
	$("#myModal embed").remove();
	$(".bpFileDiv").html("")
	$("#previewBp").removeClass();
}

function previewPdf(dom) {
	console.log();
	PDFObject.embed($(dom.target).attr("data-src"),"#previewBp");
}


